{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [vritual](https://www.zhihu.com/question/23971699?sort=created)\n",
    "- 虚函数是为了允许用基类的指针来调用子类的这个函数。\n",
    "- 纯虚函数是为了实现一个接口，起到一个规范的作用，规范继承这个类的程序员必须实现这个函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-02T07:22:21.605147Z",
     "start_time": "2020-11-02T07:22:21.599913Z"
    }
   },
   "outputs": [],
   "source": [
    "#include<iostream>\n",
    "using namespace std;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-02T07:22:25.755050Z",
     "start_time": "2020-11-02T07:22:25.71844Z"
    }
   },
   "outputs": [],
   "source": [
    "class A{\n",
    "    public:  \n",
    "    virtual void foo(){  \n",
    "        cout<<\"A::foo() is called\"<<endl;  \n",
    "    }\n",
    "};\n",
    "\n",
    "class B:public A{  \n",
    "    public:  \n",
    "        void foo(){  \n",
    "            cout<<\"B::foo() is called\"<<endl;  \n",
    "        }  \n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-02T07:22:27.103038Z",
     "start_time": "2020-11-02T07:22:27.94838Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B::foo() is called\n"
     ]
    }
   ],
   "source": [
    "B *b = new B();  \n",
    "b->foo();   // 在这里，a虽然是指向A的指针，但是被调用的函数(foo)却是B的!  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-02T07:22:29.774011Z",
     "start_time": "2020-11-02T07:22:29.767077Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A::foo() is called\n"
     ]
    }
   ],
   "source": [
    "A *a = new A();\n",
    "a->foo();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
